﻿Imports DTO
Imports System.Data.OleDb

Public Class HoaDonDAO
    Public Shared Function SelectSachLikeMaSach(ByVal mahoadon As Integer, ByVal masach As Integer) As HoaDonDTO
        Dim sql As String = "select * from CHITIETHOADON where ((MaHoaDon = " & mahoadon & ")AND(MaSach like " & masach & ") )"

        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim hd As New HoaDonDTO()
            hd.MaSach = Integer.Parse(dt.Rows(0).ItemArray(0).ToString())
            Return hd
        End If
    End Function

    Public Shared Function SelectallHoaDon() As DataTable
        Dim sql As String = "select * from HOADON"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectallHoaDonChiTiet() As DataTable
        Dim sql As String = "select * from CHITIETHOADON"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Sub Insert(ByVal hd As HoaDonDTO)
        Dim sql As String = "insert into HOADON(NgayLapHoaDon,MaKhachHang) values('" & hd.NgayLapHoaDon & "'," & hd.MaKhachHang & ")"
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub InsertChitiet(ByVal hd As HoaDonDTO)
        Dim sql As String = "insert into CHITIETHOADON(MaHoaDon,MaSach,SoLuongMua) values(" & hd.MaHoaDon & "," & hd.MaSach & "," & hd.SoLuongMua & ")"
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub Update(ByVal hd As HoaDonDTO)
        Dim sql As String = "Update  HOADON set NgayLapHoaDon =('" & hd.NgayLapHoaDon & "'),MaSach=(" & hd.MaSach & "), MaKhachHang=(" & hd.MaKhachHang & ") where MaHoaDon=" & hd.MaHoaDon & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Function Delete(ByVal hd As HoaDonDTO) As Boolean
        Try
            Dim sql As String = "delete from HOADON where MaHoaDon= " & hd.MaHoaDon & ""
            DatabassProvider.ExcuNonQuery(sql)
            Return True
        Catch
            Return False
        End Try
    End Function
End Class
